package com.lych.server.controller;

import com.lych.model.domain.User;
import com.lych.model.domain.UserInfo;
import com.lych.model.vo.CountsVo;
import com.lych.model.vo.PageResult;
import com.lych.model.vo.SettingsVo;
import com.lych.model.vo.UserInfoVo;
import com.lych.server.interceptor.UserHolder;
import com.lych.server.service.SettingsService;
import com.lych.server.service.UserInfoService;
import com.lych.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    /**
     * 用户资料 - 读取
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity users(Long userID){
        //如果userId为空，则添加token中的UserID
        if (userID == null) {
            userID = UserHolder.getUserId();
        }
        //调用service
        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     * @return
     */
    @PutMapping
    public ResponseEntity users(@RequestBody UserInfo userInfo){
        //传过来的参数没有id，需要手动添加
        userInfo.setId(UserHolder.getUserId());
        //调用service
        userInfoService.updateUserInfo(userInfo);

        return ResponseEntity.ok(null);
    }

    /**
     * 用户通用设置 - 读取
     * @return
     */
    @GetMapping("settings")
    public ResponseEntity settings(){
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题 - 保存
     * @param map
     * @return
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){
        String content = (String) map.get("content");
        settingsService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置 - 保存
     * @param map
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map){
        settingsService.saveSetting(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 黑名单 - 翻页列表
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "10") int size){

        PageResult pr = settingsService.blacklist(page,size);
        return ResponseEntity.ok(pr);
    }

    /**
     * 黑名单 - 移除
     * @param blackUserId
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity blacklistDelete(@PathVariable("uid") Long blackUserId){
        settingsService.blacklistDelete(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号- 1 发送短信验证码
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity phoneSendVerificationCode(){
        //得到当前用户手机号
        String mobile = UserHolder.getMobile();
        userService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号 - 2 校验验证码
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity phoneCheckVerificationCode(@RequestBody Map map){
        String code = (String) map.get("verificationCode");
        Boolean verification = userService.phoneCheckVerificationCode(code);
        HashMap hashMap = new HashMap();
        hashMap.put("verification",verification);
        return ResponseEntity.ok(hashMap);
    }

    /**
     * 修改手机号 - 3 保存
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity phone(@RequestBody Map map){
        String phone = (String) map.get("phone");
        Long userId = UserHolder.getUserId();
        User user = new User();
        user.setId(userId);
        user.setMobile(phone);
        userService.updatePhone(user);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        CountsVo vo = userService.counts();
        return ResponseEntity.ok(vo);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity FriendsType(@PathVariable("type") Integer type,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      String nickname){
        PageResult pr = userService.friendsType(type,page,pagesize,nickname);
        return ResponseEntity.ok(pr);
    }
}
